package sgs.common.utils;

import com.opencsv.CSVReader;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

/**
 * 文件处理工具类
 *
 * @author yaenli
 * @since 2024-05-09
 */
public class FileUtils {

    private static List<String> imageExtensions = Arrays.asList(".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".tif", ".ico", ".webp");

    /**
     * 读取csv文件
     *
     * @param fileName 文件名(带路径)
     * @param encode   文件编码格式
     * @return
     * @author yaenli
     * @since 2023-2-19
     */
    public static ArrayList<String[]> readCsv(String fileName, String encode) throws Exception {
        ArrayList<String[]> csvList = new ArrayList<String[]>();

        try (Reader reader = new InputStreamReader(new FileInputStream(fileName), Charset.forName(encode));
             CSVReader csvReader = new CSVReader(reader)) {
            csvList.addAll(csvReader.readAll());
        }

        return csvList;
    }

    public static boolean compareFiles(File file1, File file2) throws  IOException {
        return org.apache.commons.io.FileUtils.contentEquals(file1, file2);
    }

    public static boolean isImageByExtension(Path filePath) {
        String fileName = filePath.getFileName().toString().toLowerCase();
        return imageExtensions.stream().anyMatch(fileName::endsWith);
    }
}
